import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

enum Api {
  list = '/seal/sealLeadDevice/list',
  save = '/seal/sealLeadDevice/add',
  edit = '/seal/sealLeadDevice/edit',
  deleteOne = '/seal/sealLeadDevice/delete',
  deleteBatch = '/seal/sealLeadDevice/deleteBatch',
  importExcel = '/seal/sealLeadDevice/importExcel',
  exportXls = '/seal/sealLeadDevice/exportXls',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  const options = [
    { value: '610322', label: '新城区' },
    { value: '610323', label: '碑林区' },
    { value: '610324', label: '莲湖区' },
    { value: '610325', label: '灞桥区' },
    { value: '610326', label: '未央区' },
    { value: '610327', label: '雁塔区' },
    { value: '610328', label: '阎良区' },
    { value: '610329', label: '临潼区' },
    { value: '610330', label: '长安区' },
    { value: '610331', label: '高陵区' },
    { value: '610332', label: '鄠邑区' },
    { value: '610333', label: '蓝田县' },
    { value: '610334', label: '周至县' },
  ];
  if (params.region) {
    const selectedOption = options.find((option) => option.label === params.region);
    if (selectedOption) {
      params.regionCode = selectedOption.value;
    } else {
      console.warn(`未找到与 ${params.region} 对应的区县编码`);
    }
  } else {
    console.warn('params.region 不存在');
  }
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};
